package ac.il.technion.cs236635.project.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import ac.il.technion.cs236635.project.core.server.ServerJob;

public class JobsSubmitted {

	private static final JobsSubmitted _instance = new JobsSubmitted( );
	
	private Map<Long, ServerJob> jobs = new HashMap< Long, ServerJob>( );
	
	private List< Long> jobsList = new ArrayList< Long>( );

	private Logger log = Logger.getLogger( JobsSubmitted.class);
	
	private JobsSubmitted( ) {
		
	}
	
	public static synchronized JobsSubmitted getInstance( ) {
		return _instance;
	}
	
	public JobsSubmitted submit( final ServerJob job) {
		long time = System.currentTimeMillis( );
		jobs.put( time, job);
		jobsList.add( time);
		log.info( String.format( "SUBMITTED: Job with id: %s has been sumbitted at: %s.", job.getJobId( ), time));
		return this;
	}
	
	public synchronized ServerJob getNextJob( ) {
		if ( jobs.size( ) == 0)
			return null;
		return jobs.remove( jobsList.remove(0)); 
	}
}